<!--该功能已经全部完成-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户申请权限修改</title>
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css"/>
    <link href="../assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="../assets/global/plugins/simple-line-icons/simple-line-icons.min.css" rel="stylesheet" type="text/css"/>
    <link href="../assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <link href="../assets/global/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css"/>
    <link href="../assets/global/plugins/bootstrap-switch/css/bootstrap-switch.min.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" type="text/css" href="../assets/global/plugins/select2/select2.css"/>
    <link rel="stylesheet" type="text/css" href="../assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css"/>
    <link href="../assets/global/css/components.css" id="style_components" rel="stylesheet" type="text/css"/>
    <link href="../assets/global/css/plugins.css" rel="stylesheet" type="text/css"/>
    <link href="../assets/admin/layout/css/layout.css" rel="stylesheet" type="text/css"/>
    <!--    <link id="style_color" href="../assets/admin/layout/css/themes/darkblue.css" rel="stylesheet" type="text/css"/>-->
    <link href="../assets/admin/layout/css/custom.css" rel="stylesheet" type="text/css"/>

</head>

<script type="text/javascript" src="../../js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../../js/jquery.uniform.min.js"></script>
<script type="text/javascript" src="../../js/jquery.dataTables.min.js"></script>

<body onload="init()">
<div class="row">
    <div class="col-md-12" style="position: relative; height: 100%;width: 85%; left: 15px; top:15px">
        <!-- BEGIN PORTLET-->
        <div class="portlet light bordered form-fit">
            <div class="portlet-title">
                <div class="caption font-blue">
                    <i class="icon-speech font-blue"></i>
                    <span class="caption-subject bold uppercase">当前个人权限</span>
                    <span class="caption-helper"></span>
                </div>
                <div class="actions">
                    <a href="javascript:applyModifyPermission();" class="btn btn-circle btn-default btn-sm">
                        <i class="fa fa-pencil"></i> 申请修改权限 </a>
                    <a class="btn btn-circle btn-default btn-sm" href="javascript:viewPermissionApply();">
                        <i class="fa fa-search"></i>查看权限修改申请</a>
                    <a class="btn btn-circle btn-default btn-sm" href="javascript:cancelPermissionApply();">
                        <i class="fa fa-remove"></i>撤销权限修改申请</a>
                </div>
            </div>
            <div class="portlet-body form">
                <form id="form-username" class="form-horizontal form-bordered">
                    <div class="form-group">
                        <label class="col-md-3 control-label">用户名(account)</label>
                        <div class="col-md-9">
                            <textarea class="form-control autosizeme" rows="1" placeholder="..." id="userAccount" readonly></textarea>
                            <p class="help-block" id="personAccount">
                                type more to see how this autosize feature works
                            </p>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-3 control-label">用户邮箱(mail)</label>
                        <div class="col-md-9">
                            <textarea class="form-control autosizeme" rows="1" placeholder="..." id="userMail" readonly></textarea>
                            <p class="help-block" id="personMail">
                                type more to see how this autosize feature works
                            </p>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-3 control-label">用户权限(Permission)</label>
                        <div class="col-md-9">
                            <textarea class="form-control autosizeme" rows="1" placeholder="..."id="userLevel" readonly></textarea>
                            <p class="help-block" id="personLevel">
                                type more to see how this autosize feature works
                            </p>
                        </div>
                    </div>

                    <div class="form-group" style="display: none" id="modifyPermission">
                        <label class="col-md-3 control-label">修改权限(Permission)</label>
                        <div class="col-md-9">
                            <div class="form-group form-md-line-input form-md-floating-label" style="position:relative;width: 100%;">
                                <select class="form-control edited" id="modifyLevel">
                                    <option value="2" selected>普通用户</option>
                                    <option value="1">普通管理员</option>
                                    <option value="0">系统管理员</option>
                                </select>
                            </div>
                            <p class="help-block" id="modifyLevelTip">
                                type more to see how this autosize feature works
                            </p>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-md-3 control-label">注册日期(signUpDate)</label>
                        <div class="col-md-9">
                            <textarea class="form-control autosizeme" rows="1" placeholder="..." id="signUpDate" readonly></textarea>
                            <p class="help-block" id="signUpDateTip">
                                type more to see how this autosize feature works
                            </p>
                        </div>
                    </div>

                    <div style="height: 50px"></div>
                    <div class="form-actions" style="display: none" id="submitAndcancel">
                        <div class="row">
                            <div class="col-md-offset-3 col-md-9">
                                <button type="button" class="btn red" id="modifyPermissionSubmit"><i class="fa fa-check"></i> Submit</button>
                                <button type="button" class="btn default" onclick="window.location.reload()">Cancel</button>
                            </div>
                        </div>
                    </div>
                    <div id="pos" style="height: 50px"></div>
                </form>
            </div>
        </div>
        <!-- END PORTLET-->
    </div>
</div>
<!--关于权限申请的表-->
<div style="position: relative;width: 88%; left: 10px; display: none;" id="ApplyDataTable">
    <table class="table table-striped table-bordered table-hover datatable" id="record_list">
        <thead>
        <tr>
            <th class="table-checkbox"><input type="checkbox" class="group-checkable" data-set="#record_list .checkboxes" /></th>
            <th>申请用户(account)</th>
            <th>申请邮箱(mail)</th>
            <th>用户原始权限</th>
            <th>用户申请权限</th>
            <th>申请日期</th>
            <th>是否被处理?</th>
            <th>处理人等级</th>
            <th>处理人</th>
            <th>处理日期</th>
            <th>处理结果</th>
            <th>操作</th>
            <!--添加 允许/拒绝-->
        </tr>
        </thead>
    </table>
</div>
<div style="position: relative; display: none; height: 100px" id="tag"></div>
</body>
<script>
    function init(){
        let userId=sessionStorage.getItem("userId");
        let url="../../Login_ServletAction";
        let message={};
        message.Action="getPersonInfo";
        message.userId=userId;
        $.post(url,message,function (json){
            console.log(json);
            if(json.ok==200){
                initInfo(json);
            }else{
                alert("获取个人信息失败!");
            }
        })
    }

    function initInfo(json){
        document.getElementById("userAccount").innerText=json.Data[0].account;
        document.getElementById("userMail").innerText=json.Data[0].mail;
        document.getElementById("signUpDate").innerText=json.Data[0].signUpDate;
        let level=sessionStorage.getItem("level");
        if(level==0){
            document.getElementById("userLevel").innerText="系统管理员";
        }else if(level==1){
            document.getElementById("userLevel").innerText="普通管理员";
        }else{
            document.getElementById("userLevel").innerText="普通用户";
        }
    }

    function applyModifyPermission(){
        document.getElementById("modifyPermission").style.display="block";
        document.getElementById("submitAndcancel").style.display="block";
        document.getElementById("pos").style.display="block";
        document.getElementById("modifyLevelTip").innerText="请在此选择个人权限...";
    }

    // 提交申请
    document.getElementById("modifyPermissionSubmit").onclick=function (){
        let level=document.getElementById("modifyLevel").value;
        if(level==sessionStorage.getItem("level")){
            alert("尚未对权限做出修改!");
            return;
        }else {
            if(confirm("您确定要提交个人权限修改申请吗?")){
                let url="../../Login_ServletAction";
                let message={};
                message.Action="applyPersonPermission";
                message.mail=sessionStorage.getItem("mail");
                message.account=sessionStorage.getItem("account");
                message.userId=sessionStorage.getItem("userId");
                message.oldLevel=sessionStorage.getItem("level");
                message.applyLevel=level;
                $.post(url,message,function (json){
                    if(json.ok==200){
                        alert("权限修改申请提交成功!");
                        window.location.reload();
                    }else if(json.ok==500){
                        alert("权限修改申请已经存在！请等待管理员审核或撤销已经存在的申请！");
                    }else{
                        alert("权限修改申请提交失败，请稍后重试!");
                    }
                })
            }
        }
    }

    // 撤销申请
    function cancelPermissionApply(){
        let url="../../Login_ServletAction";
        let message={};
        message.Action="cancelPermissionApply";
        message.applyUserId=sessionStorage.getItem("userId");
        message.flag="cancel";
        if(confirm("您确定要撤销权限修改申请吗?")){
            $.post(url,message,function (json){
                if(json.ok==200){
                    alert("申请撤销成功!");
                    window.location.reload();
                }else if(json.ok==500){
                    alert("权限修改申请已被审核，不可撤销!")
                }else{
                    alert("申请撤销失败!请确定存在申请!")
                }
            })
        }
    }

    // 查看个人的权限申请
    function viewPermissionApply(){
        initDataTable();
        document.getElementById("ApplyDataTable").style.display="block";
        document.getElementById("tag").style.display="block";
    }

    // 用户确认权限处理结果
    function sureApplyDealResult(flag){
        let url="../../Login_ServletAction";
        let message={};
        message.Action="cancelPermissionApply";
        message.applyUserId=sessionStorage.getItem("userId");
        message.flag="sure";
        if(flag==1 && confirm("您确定要确认权限修改处理结果吗?确认之后将重新登录！")){
            $.post(url,message,function (json){
                if(json.ok==200){
                    alert("确认成功!");
                    window.parent.location.href="../login.html"
                    // window.location.reload();
                }else{
                    alert("确认处理结果失败!")
                }
            })
        }else if(flag==0){
            alert("管理员尚未处理，请等待处理之后再确认！");
        }
    }

    let resultList=[];
    let permissionArray=['系统管理员','普通管理员','普通用户']
    // 初始化表格
    function initDataTable(){
        resultList=[];
        $('.datatable').dataTable({
            "paging": true,
            "searching": true,
            "destroy":true,
            "oLanguage": {
                "aria": {
                    "sortAscending": ": activate to sort column ascending",
                    "sortDescending": ": activate to sort column descending"
                },
                "sProcessing": "处理中...",
                "sLengthMenu": "_MENU_ 记录/页",
                "sZeroRecords": "没有匹配的记录",
                "sInfo": "显示第 _START_ 至 _END_ 项记录，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项记录，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项记录过滤)",
                "sInfoPostFix": "",
                "sSearch": "查询:",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
            },
            "aoColumns": [{
                "mRender": function (data, type, full) {
                    sReturn = '<input type="checkbox" class="checkboxes" value="' + data + '"/>';
                    return sReturn;
                }, "orderable": false
            }, {
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+full.applyAccount+"</div>";
                    return sReturn;
                }, "orderable": true
            }, {
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+full.mail+"</div>";
                    return sReturn;
                }, "orderable": true
            }, {
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+permissionArray[full.oldLevel]+"</div>";
                    return sReturn;
                }, "orderable": true
            }, {
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+permissionArray[full.applyLevel]+"</div>";
                    return sReturn;
                }, "orderable": true
            }, {
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+full.applyDate+"</div>";
                    return sReturn;
                }, "orderable": true
            },{
                "mRender": function (data, type, full) {
                    sReturn ="<div>"+full.isDealed+"</div>";
                    return sReturn;
                }, "orderable": true
            },{
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+permissionArray[full.dealManageLevel]+"</div>";
                    return sReturn;
                }, "orderable": true
            },{
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+full.dealManageAccount+"</div>";
                    return sReturn;
                }, "orderable": true
            },{
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+full.dealDate+"</div>";
                    return sReturn;
                }, "orderable": true
            },{
                "mRender": function (data, type, full) {
                    sReturn = "<div>"+full.dealResult+"</div>";
                    return sReturn;
                }, "orderable": true
            },{
                "mRender": function (data, type, full) {
                    resultList.push(full);
                    let flag=-1;
                    if(full.isDealed=="yes"){
                        flag=1;
                    }else if(full.isDealed=="no"){
                        flag=0;
                    }
                    sReturn = "<a href=\"javascript:cancelPermissionApply()\">【撤销申请】</a>";
                    sReturn+= "<a href=\"javascript:sureApplyDealResult("+flag+")\">【确认】</a>";
                    return sReturn;
                }, "orderable": true
            }
            ],
            "aLengthMenu": [[5, 10, 15, 20, 25, 40, 50, -1], [5, 10, 15, 20, 25, 40, 50, "所有记录"]],
            "fnDrawCallback": function () {
                $(".checkboxes").uniform();
                $(".group-checkable").uniform();
            },
            "sAjaxSource": "../../Login_ServletAction?Action=getApplyPermission&applyUserId="+sessionStorage.getItem("userId")+"&type=person"
        });

        $('.datatable').find('.group-checkable').change(function () {
            var set = jQuery(this).attr("data-set");
            var checked = jQuery(this).is(":checked");
            jQuery(set).each(function () {
                if (checked) {
                    $(this).attr("checked", true);
                    $(this).parents('tr').addClass("active");
                } else {
                    $(this).attr("checked", false);
                    $(this).parents('tr').removeClass("active");
                }
            });
            jQuery.uniform.update(set);
        });
        $('.datatable').on('change', 'tbody tr .checkboxes', function () {
            $(this).parents('tr').toggleClass("active");
        });
    };




</script>
</html>